package com.dhcc.rarp.service.appointment.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.dhcc.framework.app.service.common.AbstractBaseService;
import com.dhcc.framework.common.PageModel;
import com.dhcc.rarp.dao.appointment.AppointmentDao;
import com.dhcc.rarp.dto.appointment.AppointmentDto;
import com.dhcc.rarp.entity.appointment.Appointment;
import com.dhcc.rarp.service.appointment.AppointmentService;

/**
* 
* <p>业务描述: 请在这里写上你的业务描述</p>
* 公司:东华医为科技有限公司<br/>
* <p>版权: dhcc2019</p>
* @author: {$GenAuthor}
* @date: {$GenDate}
* @version: V1.0
 */
 
@Service("appointmentService")
public class AppointmentServiceImpl extends AbstractBaseService<Appointment, String> implements AppointmentService {

	private AppointmentDao appointmentDao;

	public AppointmentServiceImpl(AppointmentDao appointmentDao) {
		super(appointmentDao);
		this.appointmentDao = appointmentDao;
	}

	@Override
	public PageModel page(AppointmentDto dto) {
		Specification<Appointment> spec = getListSpecification(dto);
		if (dto.getPageModel()==null) {
			dto.setPageModel(new PageModel());
		}
		Page<Appointment> page = appointmentDao.findAll(spec, dto.getPageModel().getPageable());
		dto.getPageModel().setPage(page);
		return dto.getPageModel();
	}
	
	protected Specification<Appointment> getListSpecification(final AppointmentDto dto){
        return new Specification<Appointment>() {
            @Override
            public Predicate toPredicate(Root<Appointment> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicate = new ArrayList<>();
                //构造查询条件
   
                Predicate[] pre = new Predicate[predicate.size()];
                return query.where(predicate.toArray(pre)).getRestriction();
            }
        };
	}

}
